package personnal.blogprinter;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import personnal.blogprinter.data.PostError;
import personnal.blogprinter.data.PostRange;
import personnal.blogprinter.data.Settings;
import personnal.blogprinter.exceptions.ExtractionException;
import personnal.blogprinter.exceptions.NetworkException;

public class SimpleBlogPrinter extends BlogPrinter {

	private static Logger logger = Logger.getLogger(SimpleBlogPrinter.class);
	
	public SimpleBlogPrinter(Settings settings) throws ExtractionException {
		super(settings);
	}

	public void extract() throws NetworkException, ExtractionException {

		List<PostRange> inputQueue = new LinkedList<PostRange>();
		List<PostError> errorQueue = new LinkedList<PostError>();

		fillInQueue(inputQueue);

		WordpressClient client = createClient(settings);
		BlogPostHandler handler = createBlogPostHandler(client, errorQueue);
		logger.info("Creating and starting extraction");
		long start = System.currentTimeMillis();
		for (PostRange postRange : inputQueue) {
			handler.handlePostRange(postRange);
//			Thread.sleep(1000L);
		}
		long end = System.currentTimeMillis();
		logger.info("Extraction ended (elapsed time: "+(end-start)+" ms)");
		handlePostErrors(errorQueue);
	}

	protected BlogPostHandler createBlogPostHandler(WordpressClient client, List<PostError> errorQueue) {
		return new BlogPostHandler(client, errorQueue);
	}
}
